<?php
include_once("../includes/page_utf_class.php");
include("../lang/cn/company_type_config.php");

if($_GET['type'] && $_GET['id']>0){
	$sql = "select admin_remark from ".AFTERSALES." where id='{$_GET['id']}'";
	$db->query($sql);
	$admin_remark = $db->fetchField('admin_remark');
	if($_GET['type']=='pass'){
		$sql = "select id,address from ".AFTERSALESADDRESS." where status>0";
		$db->query($sql);
		$after_address = $db->getRows();
		$tpl->assign('after_address',$after_address);
	}
	$tpl->assign('type',$_GET['type']);
	$tpl->assign('id',$_GET['id']);
	$tpl->assign('admin_remark',$admin_remark);
	$tpl->display('after_sale_opear.htm');
	exit;
}

if($_POST['type'] && $_POST['id']>0){
	$status = "";
	$set = "";
	switch ($_POST['type'])
	{
		case 'pass':
			$status = 2;break;
		case 'not_pass':
			$status = -2;break;
		case 'recieved':
			$status = 4;break;
		case 'check_yes':
			$status = 5;break;
		case 'check_no':
			$status = -3;break;
		case 'complete':
			$status = 9;break;
		case 'cancel':
			$status = -1;break;
		default:
			break;
	}
	if($status){
		$set .=" set status='{$status}'";
		if($_POST['admin_remark']!=""){
			$set .= ",admin_remark='{$_POST['admin_remark']}' ";
		}
		if($_POST['back_address']>0){
			$set .= ",back_address='{$_POST['back_address']}' ";
		}
		$sql = "update ".AFTERSALES." {$set} where id='{$_POST['id']}'";
		$db->query($sql);
	}
	msg("module.php?m=product&s=after_sale.php");
}

if($_GET['code'])
{
	$str.=" and  a.order_id like '$_GET[code]%' ";
}
if($_GET['onlytoday']){
	$str .= " and a.status='1' and a.order_time>='".strtotime(date('Y-m-d 00:00:00',time()))."'";
}else{
	if(isset($_GET['status']) and is_numeric($_GET['status']))
	{
		$status=$_GET['status'];
		$str.=" and  a.status ='$status' ";
	}
}
if($_GET['user'])
{
	$sqluser="select userid from ".MEMBER." where  username like '%$_GET[user]%'";
	$db->query($sqluser);
	$userid=$db->fetchField('userid');
	$str.=" and a.member_id='$userid'";
}
if($_GET['contact_person']){
	$str .= " and a.contact_person like '{$_GET['contact_person']}%'";
}
if($_GET['contact_mobile']){
	$str .= " and a.contact_mobile like '{$_GET['contact_mobile']}%'";
}
if($_GET['stime'])
{
	$str.=" and a.create_time > '".date('Y-m-d 00:00:00',strtotime($_GET[stime]))."' ";
}
if($_GET['etime'])
{
	$str.=" and a.create_time < '".date('Y-m-d 23:59:59',strtotime($_GET[etime]))."' ";
}
$sql="select a.*,(select name from ".MEMBER." where userid=a.member_id) as user_nick_name from ".AFTERSALES." a where 1 ".$str;
//=============================
$page = new Page;
$page->listRows=20;
if (!$page->__get('totalRows')){
	$db->query($sql);
	$de['count']=$page->totalRows = $db->num_rows();
}
$de['count']=$de['count']?$de['count']:$_GET['totalRows'];
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['page'] = $page->prompt();
//=============================

$db->query($sql);
$ss=$db->getRows();
foreach($ss as $k)
{
	$k['status_name'] = $after_sale_status[$k['status']];
	if(strlen($k['description'])>20){
		$k['description'] = mb_substr($k['description'], 0, 20,"utf-8")."...";
	}
	$list[]=$k;
}
$de["list"]=$list;

$tpl->assign('after_sale_status',$after_sale_status);
$tpl->assign('de',$de);
$tpl->display("after_sale.htm");